#region "WebUserCustomizedConfigurationManager"

/*
 * ####     Developer Name : Wang Yucai
 * 
 * ####     Development Tool : Microsoft VisualStudio 2010 Ultimate Edition
 * 
 * ####     Create Time : 2014-07-15 9:47:37
 * 
 * ####     Namespace : Masterduner.SourceStudio.Practices.DevelopmentLibrary.Web.Configuration
 * 
 * ####     Type Name : WebUserCustomizedConfigurationManager
 * 
 * ####     Full Name : Masterduner.SourceStudio.Practices.DevelopmentLibrary.Web.Configuration.WebUserCustomizedConfigurationManager
 * 
 * ####     Machine Name : GLCHQWYCWINW7
 * 
 * ####     UserName : GUOCOLAND/wangyucai
 * 
 * ####     CLR Version : 4.0.30319.18444
 * 
 * ####     Target Framework Version : 4.0
 */

#endregion

using System;
using System.Configuration;
using System.IO;
using System.Web.Configuration;
using Masterduner.SourceStudio.Practices.DevelopmentLibrary.Configuration;
using Masterduner.SourceStudio.Practices.DevelopmentLibrary.Web.Runtime;

namespace Masterduner.SourceStudio.Practices.DevelopmentLibrary.Web.Configuration
{
    /// <summary>
    /// <para>Masterduner.SourceStudio.Practices.DevelopmentLibrary.Web.Configuration.WebUserCustomizedConfigurationManager</para>
    /// <para>
    /// 用于管理用户自定义配置。
    /// </para>
    /// </summary>
    /// <remarks>
    /// <para>Target Framework Version : 4.0</para>
    /// </remarks>
    public class WebUserCustomizedConfigurationManager : UserCustomizedConfigurationManager
    {
        #region Constructor

        /// <summary>
        /// <para>构造函数：</para>
        /// <para>初始化一个<see cref="WebUserCustomizedConfigurationManager" />对象实例。</para>
        /// </summary>
        /// <param name="sectionName">自定义配置节（组）名称。</param>
        /// <param name="isSectionGroup">是否为配置节（组）。</param>
        public WebUserCustomizedConfigurationManager(string sectionName, bool isSectionGroup = false)
            : base(sectionName, isSectionGroup)
        {
        }

        #endregion

        #region LoadFromWebConfig
        /// <summary>
        /// 从web.config中获取<see cref="UserCustomizedConfigurationFilesSection"/>配置对象实例。
        /// </summary>
        /// <returns><see cref="UserCustomizedConfigurationFilesSection"/>配置对象实例。</returns>
        protected virtual UserCustomizedConfigurationFilesSection LoadFromWebConfig()
        {
            try
            {
                ConfigurationSection section = WebConfigurationManager.OpenWebConfiguration(null, AspContextVariables.Context.SiteName).Sections[UserCustomizedConfigurationFilesSection.SectionName];
                return section.IsNull() ? null : section.ForceTo<UserCustomizedConfigurationFilesSection>();
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region LoadFromAppConfig
        /// <summary>
        /// 从web.config中获取<see cref="UserCustomizedConfigurationFilesSection"/>配置对象实例。
        /// </summary>
        /// <returns><see cref="UserCustomizedConfigurationFilesSection"/>配置对象实例。</returns>
        protected override UserCustomizedConfigurationFilesSection LoadFromAppConfig()
        {
            return base.LoadFromAppConfig();
        }
        #endregion

        #region CreateUserCustomizedConfigurationFileInfo
        /// <summary>
        /// 创建用户自定义配置文件信息。
        /// </summary>
        /// <param name="element"><see cref="UserCustomizedConfigurationFileElement"/>对象实例。</param>
        /// <returns><see cref="FileInfo"/>对象实例。</returns>
        protected override FileInfo CreateUserCustomizedConfigurationFileInfo(UserCustomizedConfigurationFileElement element)
        {
            if (element.Directory.IsEmptyOrWriteSpaces() || element.Equals(@"\"))
                return new FileInfo(Path.Combine(string.Format(@"{0}\", AspContextVariables.Context.CurrentRunningDirectory.TrimEnd('\\')), element.FileName.IsEmptyOrWriteSpaces() ? "UserCustomized.config" : element.FileName));
            else
                return new FileInfo(Path.Combine(element.Directory, element.FileName.IsEmptyOrWriteSpaces() ? "UserCustomized.config" : element.FileName));
        }
        #endregion
    }
}

#region README

/*
 * ####     ©2014 Wang Yucai
 * 
 * ####     Contact me
 *                  
 *                  E-mail : Masterduner@Yeah.net
 *                  Mobile : 13621384677
 *                  QQ : 180261899
 */

#endregion